home *** CD-ROM | disk | FTP | other *** search
/ 9-Digit Zip Code Directory / 9-Digit Zip Code Directory (American Business Information) (ABIZIP-12).ISO / z4src.zip / Z4PFCP.C < prev    next >
C/C++ Source or Header  |  1993-07-26  |  6KB  |  221 lines

  1. //----------------------------------------------------------------------------
  2. //                            MODULE DESCRIPTION
  3. //
  4. //  Module:    z4pfcp.c
  5. //   Title:    ZIP+4 Engine
  6. //  Notice:    John M. Weeder
  7. //                 Copyright (c) 1993. All rights reserved.
  8. //             This module contains proprietary information and should be 
  9. //                treated as confidential.
  10. //
  11. //----------------------------------------------------------------------------
  12. //                           MAINTENANCE HISTORY
  13. //
  14. // $Workfile$
  15. // $Revision$
  16. //   $Author$
  17. //     $Date$
  18. //      $Log$    
  19. //
  20. //----------------------------------------------------------------------------
  21. //                             MODULE NARRATIVE
  22. //
  23. //
  24. //    This module contains the compressor for the POF file.
  25. //
  26. //    The code in this module should be written entirely in C. 
  27. //    Do not use any C++ constructs.
  28. //
  29. //    This module is portable to:
  30. //        DOS 3.X+
  31. //        MS Windows 3.X+
  32. //        OS/2 2.X+
  33. //        OS/2 2.0 PM
  34. //        SCO UNIX.
  35. //
  36. //    The following compilers are supported:
  37. //        MSC 6.0A
  38. //        MSC/C++ 7.0
  39. //        Borland C++ 3.1 for DOS
  40. //        Borland C++ 1.0 for OS/2 2.X
  41. //        SCO UNIX cc
  42. //
  43. //----------------------------------------------------------------------------
  44. #include <z4.h>
  45.  
  46.  
  47. //----------------------------------------------------------------------------
  48. //    Prototypes
  49. //----------------------------------------------------------------------------
  50. static BOOL FN_L Z4PFCompressAppend(PDATACOMP, PZ4_PF);
  51. static SIZET FN_L Z4PFCompressZip3(PZ4_PF);
  52.  
  53.  
  54. //----------------------------------------------------------------------------
  55. //   Description:    Compression function    
  56. //    Parameters:    pdatacomp    Compressor data
  57. //       Returns:    Compression function result code. See data.h.
  58. //----------------------------------------------------------------------------
  59. SHORT FN_E Z4PFCompress(PDATACOMP pdatacomp)
  60. {
  61. static BOOL fPending;                        // ZIP5 record pending?
  62. static Z4_PF pf;                                // ZIP5 record
  63. static SIZET cMaxZips;
  64. static SIZET cMaxZip3;
  65. static CHAR szZip5[MAX_ZIP5+1];
  66. static LONG lPofs;
  67. static LONG lMultiPofs;
  68. static LONG l2Pofs;
  69. static SIZET cZip;
  70. static SIZET cPof;
  71.  
  72.     LONG lRec = pdatacomp->dlmrec.lId;
  73.     PPSZ ppsz = pdatacomp->dlmrec.apsz;
  74.     SIZET cZip3;
  75.  
  76.     switch (lRec)
  77.         {
  78.         case DAI_INITIALIZE:                    // Initialize at startup
  79.             cMaxZips = 0;
  80.             cMaxZip3 = 0;
  81.             lPofs = 0;
  82.             l2Pofs = 0;
  83.             lMultiPofs = 0;
  84.             Assert(MAX_POF_ZIP5 <= pdatacomp->cbMax / MAX_ZIP5_BCD);
  85.             cZip = DataField(pdatacomp->dlmrec.pcfg, "zipcode");
  86.             cPof = DataField(pdatacomp->dlmrec.pcfg, "finance");
  87.         case DAI_START_BLK:                    // Start block
  88.             fPending = FALSE;
  89.             return DAO_SUCCESS;
  90.  
  91.         case DAI_TERMINATE:                    // Terminate
  92.             Output("\n%ld POFs.\n", lPofs);
  93.             Output("%ld POFs w/ > 1 3 digit ZIP.\n", lMultiPofs);
  94.             Output("Maximum of %u ZIP5s in a POF.\n", cMaxZips);
  95.             Output("Maximum of %u ZIP3s in a POF.\n", cMaxZip3);
  96.         case DAI_FAILURE:
  97.         case DAI_END_BLK:                        // End block
  98.             return DAO_SUCCESS;
  99.  
  100.         case DAI_LAST_REC:
  101.             if (!fPending)
  102.                 return DAO_FAILURE;
  103.  
  104.             Z4PFCompressAppend(pdatacomp, &pf);
  105.             lPofs++;
  106.  
  107.             cZip3 = Z4PFCompressZip3(&pf);
  108.             cMaxZip3 = MAX(cMaxZip3, cZip3);
  109.             if (cZip3 > 1)
  110.                 {
  111.                 if (cZip3 == 2)
  112.                     l2Pofs++;
  113.                 lMultiPofs++;
  114.                 }
  115.             return DAO_FLUSH;
  116.         }
  117.     if (pdatacomp->dlmrec.fSkipped)
  118.         return DAO_SKIP;
  119.     if (fPending)
  120.         {
  121.         if (strcmp(pf.szFinance, ppsz[cPof]) != 0)
  122.             {
  123.             Z4PFCompressAppend(pdatacomp, &pf);
  124.             lPofs++;
  125.  
  126.             cZip3 = Z4PFCompressZip3(&pf);
  127.             cMaxZip3 = MAX(cMaxZip3, cZip3);
  128.             if (cZip3 > 1)
  129.                 {
  130.                 if (cZip3 == 2)
  131.                     l2Pofs++;
  132.                 lMultiPofs++;
  133.                 }
  134.             fPending = FALSE;
  135.             return DAO_MARK_FLUSH;
  136.             }
  137.         if (strcmp(szZip5, ppsz[cZip]) != 0)
  138.             {
  139.             Assert(pf.cZip5 < MAX_POF_ZIP5);
  140.             strcpy(szZip5, ppsz[cZip]);
  141.             stra2b(pf.abZip5[pf.cZip5], MAX_ZIP5_BCD, ppsz[cZip], MAX_ZIP5);
  142.             pf.cZip5++;
  143.             cMaxZips = MAX(cMaxZips, pf.cZip5);
  144.             }
  145.         return DAO_NEXT;
  146.         }
  147.     fPending = TRUE;
  148.     memset(&pf, 0, sizeof(pf));
  149.     strcpy(pf.szFinance, ppsz[cPof]);
  150.     strcpy(szZip5, ppsz[cZip]);
  151.     stra2b(pf.abZip5[pf.cZip5], MAX_ZIP5_BCD, ppsz[cZip], MAX_ZIP5);
  152.     pf.cZip5++;
  153.     cMaxZips = MAX(cMaxZips, pf.cZip5);
  154.     return DAO_NEXT;
  155. }
  156.  
  157.  
  158. //----------------------------------------------------------------------------
  159. //   Description:    Compress the current ZIP5 record
  160. //    Parameters:    pdatacomp    Compresser data
  161. //                        ppf            POF record
  162. //       Returns:    TRUE if successful.
  163. //----------------------------------------------------------------------------
  164. static BOOL FN_L Z4PFCompressAppend(PDATACOMP pdatacomp, PZ4_PF ppf)
  165. {
  166.     PBYTE pb = pdatacomp->pb;
  167.     SIZET cb;
  168.  
  169.     stra2b(pb, MAX_FINANCE_BCD, ppf->szFinance, MAX_FINANCE);
  170.     Assert(pb[0] != 0);                        // Expander will exit on a 0
  171.     pb += MAX_FINANCE_BCD;
  172.  
  173.     *(PUSHORT)pb = (USHORT)ppf->cZip5;    // Store ZIP count
  174.     pb += sizeof(USHORT);
  175.  
  176.     cb = MAX_ZIP5_BCD * ppf->cZip5;        // Store ZIPs
  177.     memcpy(pb, ppf->abZip5, cb);
  178.     pb += cb;
  179.                                                       // Update number of bytes written
  180.     pdatacomp->cb = (SIZET)(pb - pdatacomp->pb);
  181.     return TRUE;
  182. }
  183.  
  184.  
  185. //----------------------------------------------------------------------------
  186. //   Description:    
  187. //    Parameters:    ppf            POF record
  188. //       Returns:    Number of 3 digit ZIP codes in a POF
  189. //----------------------------------------------------------------------------
  190. static SIZET FN_L Z4PFCompressZip3(PZ4_PF ppf)
  191. {
  192.     CHAR szZip5a[MAX_ZIP5+1],szZip5b[MAX_ZIP5+1];
  193.     SIZET i, j, k;
  194.  
  195.     szZip5a[MAX_ZIP5] = '\0';
  196.     szZip5b[MAX_ZIP5] = '\0';
  197.  
  198.     k = 0;
  199.     for (i = 0; i < ppf->cZip5; ++i)
  200.         {
  201.         BOOL fDuplicate = FALSE;
  202.  
  203.         strb2a(ppf->abZip5[i], MAX_ZIP5_BCD, szZip5a, MAX_ZIP5, TRUE);
  204.         for (j = 0; j < i; ++j)
  205.             {
  206.             strb2a(ppf->abZip5[j], MAX_ZIP5_BCD, szZip5b, MAX_ZIP5, TRUE);
  207.             if (memcmp(szZip5a, szZip5b, 3) == 0)
  208.                 {
  209.                 fDuplicate = TRUE;
  210.                 break;
  211.                 }
  212.             }
  213.         if (!fDuplicate)
  214.             k++;
  215.         }
  216.     return k;
  217. }
  218. //----------------------------------------------------------------------------
  219. //------------------------------- End of File --------------------------------
  220. //----------------------------------------------------------------------------
  221.